#-*- coding:utf-8 -*-
from django.shortcuts import render_to_response
from models import news
from Graduation_web.views import get_msg_count

def news_action(request):
    sign = 'news'
    empty = False
    perpage = 20
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    page = request.GET.get('page','1')
    type = request.GET.get('type','')
    if type == 'previous':
        page = int(page)-1
    if type == 'next':
        page = int(page)+1
    page = int(page)
    news_all = news.objects.all()
    pages = len(news_all)/perpage + int(len(news_all)%perpage > 0)
    if page > pages:
        page = pages
    if len(news_all) > 0:
        news_all = news_all[(page-1)*perpage:page*perpage]
    else:
        empty = True
    url = 'news'
    return render_to_response('news.html',locals())

def news_single_action(request,no):
    sign = 'news'
    username = request.session.get('user','')
    msg_count = get_msg_count(username)
    words = 60
    try:
        new = news.objects.get(id = int(no))
    except :
        pass
    title = new.title
    body = new.body
    bodylist = body.split("。 ".decode('utf-8'))
    return render_to_response('new_single.html',locals())